/*
voice.c
audio player function
designed by linxiantao

--------------------------------
2020-05-06 10:39:00
create file
--------------------------------
*/

#include "../../middleware/uvoice/include/uvoice.h"
#include "fsl_wm8960.h"
#include "stdio.h"

static uvoice_player_t *uvoice_player = NULL;
static int voice_init=0;
static int play_vol=50;

/**
 * @brief 清除已设置的音频文件资源，在重新设置音频文件前时调用
 * 
 * @return int32_t 0表示成功，非0则失败。
 */
int32_t xp_voice_clr_source()
{
	int32_t ret = 0;
	if(voice_init==0)return -1;
	ret = uvoice_player->clr_source();
	if(0 != ret){
		return -1;
	}

	return 0;
}

/**
 * @brief 设置指定音频文件资源
 * 
 * @param file_name 指定的音频文件路径，sd卡上需要加上前缀fs:，
 *                  例如"fs:/sdcard/voice/test.mp3"
 * @return int32_t 0表示成功，非0则失败。
 */
static int32_t xp_voice_set_source(char *file_name)
{
	int32_t ret = 0;
	if(voice_init==0)return -1;
	ret = uvoice_player->set_source(file_name);
	if(0 != ret){
		return -1;
	}

	return 0;
}

/**
 * @brief 设置播放的音量，指定范围是0-63
 * 
 * @param volume 音量大小
 * @return int32_t 0表示成功，非0则失败。
 */
int xp_voice_set_volume(int volume)
{
	int32_t ret = 0;
	if(voice_init==0)return -1;
	if(volume!=play_vol){
		println("set voice volume: %d.",volume);
		play_vol=volume;
	}
	ret = uvoice_player->set_volume(volume);
	if(0 != ret){
		return -1;
	}

	return 0;
}

/**
 * @brief 停止播放，并清除指定音频文件资源
 * 
 * @return int32_t 0表示成功，非0则失败。
 */
int32_t xp_voice_stop()
{
	int32_t ret = 0;
	if(voice_init==0)return -1;
	println("stop voice play.");
	if(NULL == uvoice_player){
		return -1;
	}

	ret = uvoice_player->stop();
	if(0 != ret){
		return -1;
	}

	ret = xp_voice_clr_source();
	if(0 != ret){
		return -1;
	}

	return 0;
}

/**
 * @brief 设置播放的音量，指定范围是0-63
 * 
 * @param file_name 指定的音频文件路径，sd卡上需要加上前缀fs:，
 *                  例如"fs:/sdcard/voice/test.mp3"
 * @param volume 音量大小
 * @return int32_t 0表示成功，非0则失败。
 */
int32_t xp_voice_start(char *file_name, int32_t volume)
{
	int32_t ret = 0;
	if(voice_init==0)return -1;
	if(NULL == uvoice_player){
		return -1;
	}

	if(volume!=0){
		ret = xp_voice_set_volume(play_vol);
		if(0 != ret){
			return -1;
		}
	}

	println("play music: %d volume, %s.",volume,file_name);
	ret = xp_voice_set_source(file_name);
	if(0 != ret){
		return -1;
	}
	ret = uvoice_player->start();
	if(0 != ret){
		return -1;
	}	

	return 0;
}

/**
 * @brief 音频播放器初始化
 * 
 * @return int32_t 0表示成功，非0则失败。
 */
int32_t xp_voice_init()
{
	static int init=0;
	int32_t ret = 0;
	if(init==1)return 0;
	ret = uvoice_init();
	if(0 != ret){
		return -1;
	}

	uvoice_player = uvoice_player_create();
	if(NULL != ret){
		return -1;
	}
	xp_voice_set_volume(60);
	init=1;
	voice_init=1;
	return 0;
}


/*****************************/
/*
puase play
designed by lunanting
return:
	0=success
*/
int xp_voice_pause(void){
	if(voice_init==0)return -1;
	if(uvoice_player->pause()!=0)return -1;
	return 0;
}

/*
resume play
designed by lunanting
return:
	0=success
*/
int xp_voice_resume(void){
	if(voice_init==0)return -1;
	if(uvoice_player->resume()!=0)return -1;
	return 0;
}

/*********************************voice debug************************************/
//voice debug
int xp_voice_debug(char *type,char *fun,char *param){
	static char file[50];

	if(strcmp(type,"voice")!=0)return 0;
	if(strcmp(fun,"play")==0){
		sprintf(file,"fs:/sdcard/%s",param);
		xp_voice_clr_source();
		xp_voice_start(file,50);

	}
	else if(strcmp(fun,"stop")==0){
		xp_voice_stop();
	}
	else if(strcmp(fun,"volume")==0){
		xp_voice_set_volume(atoi(param));
	}
	else if(strcmp(fun,"pause")==0){
		xp_voice_pause();
	}
	else if(strcmp(fun,"resume")==0){
		xp_voice_resume();
	}
	else{
		return 0;
	}
	return 1;
}




